package mulegame;

import java.awt.Color;
/**
 * Makes tile objects, can set and retrieve info about them, can pick up/park mule, set the image path and change 
 * color based on whether it's owned. 
 * 
 * @author AREA 29
 *
 */
public class Tile {
	private TileType terrainType;
	private boolean muleParked;
	private Mule mule;
	private Player owner;
	private Color borderColor;
	private String currentImagePath;
   /**
    * makes a new tile object of terrain type t and without a mule
    * 
    * @param t tile type
    */
	public Tile(TileType t) {
		terrainType = t;
		muleParked = false;
		owner = null;
		mule = null;
	}
	
	/**
	 * returns the terrain type
	 * 
	 * @return terrainType
	 */
	public TileType getTerrainType() {
		return terrainType;
	}
	
   /**
    * updates the terrain type and returns true to tell user it has been changed
    * 
    * @param t tile type
    * @return true
    */
	public boolean updateTerrainType(TileType t) {
		terrainType = t;
		return true;
	}
	
   /**
    * if the tile type isn't town and there is no mule on it, change the image path to tile with a mule and return
    * true that the mule has been parked, otherwise return false 
    * 
    * @return muleParked
    */
	
	public boolean placeMule(Mule m) {
		// makes sure it is NOT the TOWN, and there is no MULE there
		if (terrainType != TileType.TOWN && muleParked == false) {
			muleParked = true;
			mule = m;
			String newImagePath = "src/mulegame/Images/P1T.jpg";
//			String oldImagePath = getImagePath();
//			String newImagePath = oldImagePath.replace("F.jpg", "T.jpg");    //TODO this is a hack, we should do this better
			switch(mule){
			case FOOD:
			    switch(terrainType){
			    case PLAIN:
			        newImagePath = "src/mulegame/Images/P1FT.jpg";
				    break;
			    case RIVER:
			    	newImagePath = "src/mulegame/Images/R1FT.jpg";
			    	break;
			    case M1:
			    	newImagePath = "src/mulegame/Images/M1FT.jpg";
			    	break;
			    case M2:
			    	newImagePath = "src/mulegame/Images/M2FT.jpg";
			    	break;
			    case M3:
			    	newImagePath = "src/mulegame/Images/M3FT.jpg";
			    	break;
			    }
				break;
			case ENERGY:
				switch(terrainType){
			    case PLAIN:
			        newImagePath = "src/mulegame/Images/P1ET.jpg";
				    break;
			    case RIVER:
			    	newImagePath = "src/mulegame/Images/R1ET.jpg";
			    	break;
			    case M1:
			    	newImagePath = "src/mulegame/Images/M1ET.jpg";
			    	break;
			    case M2:
			    	newImagePath = "src/mulegame/Images/M2ET.jpg";
			    	break;
			    case M3:
			    	newImagePath = "src/mulegame/Images/M3ET.jpg";
			    	break;
			    }
				break;
			case ORE:
				switch(terrainType){
			    case PLAIN:
			        newImagePath = "src/mulegame/Images/P1BT.jpg";
				    break;
			    case RIVER:
			    	newImagePath = "src/mulegame/Images/R1BT.jpg";
			    	break;
			    case M1:
			    	newImagePath = "src/mulegame/Images/M1BT.jpg";
			    	break;
			    case M2:
			    	newImagePath = "src/mulegame/Images/M2BT.jpg";
			    	break;
			    case M3:
			    	newImagePath = "src/mulegame/Images/M3BT.jpg";
			    	break;
			    }
				break;
			default:
				newImagePath = getImagePath();
			}
				
			setImagePath(newImagePath);
			return muleParked;
		}  else {
			return false;
		}
	}

	/**
	 * if the tile type isn't town and there is a mule on it, change the image path to tile without a mule and return
     * true that the mule has been unparked, otherwise return false 
	 * 
	 * @return !muleParked
	 */
	public boolean pickUpMule() {
		//makes sure it is NOT the TOWN, and there is a MULE there
		if (terrainType != TileType.TOWN && muleParked == true) {
			muleParked = false;
			String oldImagePath = getImagePath();
			String newImagePath = oldImagePath.replace("1.jpg", "F.jpg");    //TODO this is a hack, we should do this better
			newImagePath = oldImagePath.replace("2.jpg", "F.jpg");    //TODO this is a hack, we should do this better
			newImagePath = oldImagePath.replace("3.jpg", "F.jpg");    //TODO this is a hack, we should do this better
			setImagePath(newImagePath);
			return !muleParked;
		} else {
			return false;
		}
	}
	
   /**
    * returns amount of food produced by the tile
    * 
    * @return terrainType.getFoodProd()
    */
	public int getFoodProd() {
		// don't need to check if Tile is TOWN, because shouldn't ever ask the TOWN 
		// TOWN returns 0 anyways
		return terrainType.getFoodProd();
	}
    /**
    * returns amount of energy produced by the tile
    * 
    * @return terrainType.getEnergyProd()
    */
	public int getEnergyProd() {
		// don't need to check if Tile is TOWN, because shouldn't ever ask the TOWN 
		// TOWN returns 0 anyways
		return terrainType.getEnergyProd();
	}
	
	/**
     * returns amount of ore produced by the tile
	 * 
	 * @return terrainType.getOreProd()
	 */
	public int getOreProd() {
		// don't need to check if Tile is TOWN, because shouldn't ever ask the TOWN 
		// TOWN returns 0 anyways
		return terrainType.getOreProd();
	}
	
   /**
    * returns image path of the tile
    * 
    * @return terrainType.getImagePath()
    */
	public String getImagePath() {
	    //TODO this is hack-y, we should fix
	    if(currentImagePath == null){
	        return terrainType.getImagePath();
	    }
	    else{
	        return currentImagePath;
	    }
	}

	/**
	 * sets image path of tile
	 * 
	 * @param imageP image path 
	 */
	public void setImagePath(String imageP) {
		currentImagePath = imageP;
	}
	
	/**
	 * sets color of tile border to show who owns it 
	 * 
	 * @param p player
	 */
	public void setOwned(Player p){
	    owner = p;
	    borderColor = p.getColor();
	}
	
	/**
	 * returns true if tile is owned
	 * 
	 * @return !(owner == null)
	 */
	public boolean isOwned(){
	     return !(owner == null);   
	}
	
	/**
	 * returns owner 
	 * @return owner
	 */
	public Player getOwner(){
	     return owner;   
	}
	
	/**
	 * returns who an item is owned by
	 * @param p player
	 * @return true if it's owned by player
	 */
	public boolean isOwnedBy(Player p){
	    return owner == p;
	}
	
	/**
	 * returns whether the mule is parked
	 * @return true if mule is parked
	 */
	public boolean hasMule(){
	     return muleParked;   
	}
	
	/**
	 * returns mule object on tile
	 * @return mule
	 */
	public Mule getMule(){
	     return mule;   
	}
	
	/**
	 * returns border color
	 * 
	 * @return borderColor
	 */
	public Color getColor(){
	    return borderColor;
	}
	
	public String toString(){
	    String ret = "";
	    ret += "Type : " + terrainType.toString() + "\n";
	    if(isOwned()){
	        ret += "Owner : " + owner.getName() + "\n";
	    }
	    if(muleParked){
	        ret += "Mule : " + mule.toString() + "\n";
	    }
	    return ret;
	}
}
